#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    clickTimes=0;
    mode0=true;
    ui->cut->setVisible(false);
    ui->save->setVisible(false);
    pic=QImage(0,0,QImage::Format_RGB32);
    ui->mode->setVisible(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *) {

    if(pic.width()!=0)
    {
        QRectF target(0, 0, this->width(), this->height());
        QRectF src(0, pic.height()-this->height(), this->width(), this->height());
        QPainter painter1(this);
        painter1.drawImage(target, pic, src);
    }

}
void MainWindow::on_confirm_clicked()
{
    int x=this->x();
    int y=this->y();
    int iTitleBarHeight = style()->pixelMetric(QStyle::PM_TitleBarHeight);
    int h=this->height()+iTitleBarHeight;
    int w=this->width();

    if(clickTimes==0){
        char str[100];
        sprintf(str,"(x,y):(%d,%d) (w,h):(%d,%d)",x,y,w,h);
        ui->info->setText(tr(str));
        show1.init(x,y,w,h);
        ui->cut->setVisible(true);
        ui->save->setVisible(true);
        ui->mode->setVisible(true);
        ui->confirm->setText(tr("ConfirmPlace"));
        initx=x;
        inity=y;
//        this->setMaximumWidth(w);
//        this->setMinimumWidth(w);
    }
    else if(clickTimes!=0)
    {
        if(mode0)
            show1.setPlace(x,y,h);
        else
            show1.setPlace(initx,y,h);
    }
    clickTimes++;
}

void MainWindow::on_cut_clicked()
{
    show1.close();
    this->close();
    QPixmap fullScreen;
    fullScreen = QPixmap::grabWindow(QApplication::desktop()->winId(),show1.x(),show1.y(),show1.width(),show1.height());
    show1.show();
    this->show();

    QImage src=fullScreen.toImage();
    if(mode0)
        combineImage(src);
    else
        combindImageDetail(src);

    char str[100];
    sprintf(str,"picture size: %d %d",pic.width(),pic.height());
    ui->info->setText(tr(str));
    this->update();
}

void MainWindow::combineImage(QImage src)
{
    QImage temp(src.width(),src.height()+pic.height(),src.format());

    int i=0,j=0;
    for(i=0;i<pic.width();i++)
    {
        for(j=0;j<pic.height();j++)
        {
            temp.setPixel(i,j,pic.pixel(i,j));
        }
    }

    for(i=0;i<src.width();i++)
    {
        for(j=0;j<src.height();j++)
        {
            temp.setPixel(i,j+pic.height(),src.pixel(i,j));
        }
    }

    pic=QImage(temp);
    pic.save("temp.png","png");
}

void MainWindow::on_save_clicked()
{
    QTime time(QTime::currentTime());
    int hour = time.hour();
    int minute = time.minute();
    int second = time.second();
    char name[100];
    sprintf(name,"cut_%d_%d_%d.png",hour,minute,second);
    pic.save(name,"png");
}

void MainWindow::on_cut2_clicked()
{
    int iTitleBarHeight = style()->pixelMetric(QStyle::PM_TitleBarHeight);
    int h=this->height()+iTitleBarHeight;
    int w=this->width();

    show1.close();
    this->setVisible(false);
    QPixmap fullScreen;
    fullScreen = QPixmap::grabWindow(QApplication::desktop()->winId(),this->x(),this->y()+h,w,h);
    show1.show();
    this->setVisible(true);

    QImage src=fullScreen.toImage();
    combineImage(src);

    char str[100];
    sprintf(str,"picture size: %d %d",pic.width(),pic.height());
    ui->info->setText(tr(str));
    this->update();
}





void MainWindow::combindImageDetail(QImage src)
{
    int h=detectOverlap(src);

    QImage temp(src.width(),src.height()+h,src.format());

    int i=0,j=0;
    for(i=0;i<pic.width();i++)
    {
        for(j=0;j<h;j++)
        {
            temp.setPixel(i,j,pic.pixel(i,j));
        }
    }

    for(i=0;i<src.width();i++)
    {
        for(j=0;j<src.height();j++)
        {
            temp.setPixel(i,j+h,src.pixel(i,j));
        }
    }

    pic=QImage(temp);
    pic.save("temp.png","png");
}

bool MainWindow::isSameLine(QImage img1, QImage img2, int h1, int h2)
{
    for(int i=0;i<img1.width();i++)
    {
        QRgb c1=img1.pixel(i,h1);
        QRgb c2=img2.pixel(i,h2);
        if(c1!=c2)
            return false;
    }
    return true;
}

int MainWindow::detectOverlap(QImage src)
{
    int detectH=10,c=0,j=0;
    if(pic.height()<detectH)
        return pic.height();

    for(j=pic.height()-detectH;j>=0;j--)
    {
        for(c=0;c<detectH;c++)
        {
            if(!isSameLine(pic,src,j+c,c))
                break;
        }
        if(c==detectH)
        {
            ui->info2->setText(tr("Overlap height: ")+QString::number(j));
            break;
        }
    }

    if(j<0)
        return pic.height();
    else
        return j;
}


void MainWindow::on_mode_clicked()
{
    if(mode0)
    {
        mode0=false;
        ui->mode->setText(tr("mode1"));
        ui->info2->setText(tr("Overlap Height is 10pt at least!"));
    }
    else
    {
        mode0=true;
        ui->mode->setText(tr("mode0"));
        ui->info->setText(tr("Direct Combine two images every times!"));
    }
}
